﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Web;
using DevExpress.Data.Linq;
using WhereIam.Dbs;

namespace WhereIam.Entities
{
    public class DailyLocationStat
    {
        [DisplayName("员工")]
        public string EmployeName { get; set; }

        [DisplayName("年度")]
        public int Year { get; set; }

        [DisplayName("月份")]
        public int Yue { get; set; }

        [DisplayName("01")]
        public string day1 { get; set; }

        [DisplayName("02")]
        public string day2 { get; set; }

        [DisplayName("03")]
        public string day3 { get; set; }

        [DisplayName("04")]
        public string day4 { get; set; }

        [DisplayName("05")]
        public string day5 { get; set; }

        [DisplayName("06")]
        public string day6 { get; set; }

        [DisplayName("07")]
        public string day7 { get; set; }

        [DisplayName("08")]
        public string day8 { get; set; }

        [DisplayName("09")]
        public string day9 { get; set; }

        [DisplayName("10")]
        public string day10 { get; set; }

        [DisplayName("11")]
        public string day11 { get; set; }

        [DisplayName("12")]
        public string day12 { get; set; }

        [DisplayName("13")]
        public string day13 { get; set; }

        [DisplayName("14")]
        public string day14 { get; set; }

        [DisplayName("15")]
        public string day15 { get; set; }

        [DisplayName("16")]
        public string day16 { get; set; }

        [DisplayName("17")]
        public string day17 { get; set; }

        [DisplayName("18")]
        public string day18 { get; set; }

        [DisplayName("19")]
        public string day19 { get; set; }

        [DisplayName("20")]
        public string day20 { get; set; }

        [DisplayName("21")]
        public string day21 { get; set; }

        [DisplayName("22")]
        public string day22 { get; set; }

        [DisplayName("23")]
        public string day23 { get; set; }

        [DisplayName("24")]
        public string day24 { get; set; }

        [DisplayName("25")]
        public string day25 { get; set; }

        [DisplayName("26")]
        public string day26 { get; set; }

        [DisplayName("27")]
        public string day27 { get; set; }

        [DisplayName("28")]
        public string day28 { get; set; }

        [DisplayName("29")]
        public string day29 { get; set; }

        [DisplayName("30")]
        public string day30 { get; set; }

        [DisplayName("31")]
        public string day31 { get; set; }
    }

    public class DailyLocationStatBll
    {
        DailyLocationBll bll = new DailyLocationBll();

        public List<DailyLocationStat> GetStats(int nian, int yue)
        {
            nian = nian == 0 ? DateTime.Now.Year : nian;
            yue = yue == 0 ? DateTime.Now.Month : yue;
            string condi =  string.Format(" AND YEAR(Today)={0} AND MONTH(Today)={1}", nian, yue);

            var listDl = bll.GetByCondition("*", condi);

            var employes = listDl.GroupBy(o => o.EmployeeName).Select(o => o.Key);

            List<DailyLocationStat> listdls = new List<DailyLocationStat>();

            foreach (var key in employes)
            {
                DailyLocationStat obj = new DailyLocationStat();
                obj.EmployeName = key;
                obj.Year = nian;
                obj.Yue = yue;
                var listE = listDl.Where(o => o.EmployeeName == key);
                
                foreach (var ob in listE)
                {
                    int day = ob.Today.Value.Day;
                    foreach (PropertyInfo Property in typeof(DailyLocationStat).GetProperties())
                    {
                        if (Property.Name == "day" + ob.Today.Value.ToString("dd"))
                        {
                            Property.SetValue(obj, ob.LocationName);
                        }
                    }
                    
                }
                listdls.Add(obj);
            }
            return listdls;
        }
    }
}